package com.liruo.learn.spring.mvc.enumconverter;

import org.springframework.core.convert.converter.Converter;

public  class IntToEnumConverter<T extends SourceEnum<Integer>> implements Converter<Integer, T> {
    private final Class<T> enumClazz;

    public IntToEnumConverter(Class<T> enumClazz) {
        this.enumClazz = enumClazz;
    }
    @Override
    public T convert(Integer source) {
        for (T enumeration : enumClazz.getEnumConstants()){
            if(enumeration.getSource().equals(source)){
                return enumeration;
            }
        }
        throw new IllegalArgumentException("has not the enumeration source[class=" +
                Integer.class.getName() + "] value=" + source);
    }
}